/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package xmppbot.parse;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import org.jivesoftware.smack.XMPPException;
import xmppbot.Main;
import xmppbot.db;

/**
 *
 * @author lian
 */
public class NormPacket extends packet {    public NormPacket(options opts) throws SQLException, XMPPException {
        this.opts = opts;
        init();
        process();
        ansver(null);
        log();
    }
        
    @Override
    @SuppressWarnings("unchecked")
    public void process() throws SQLException {
        //System.out.println(permittable);        String From = opts.getFrom();
        if (opts.getExecutable()) {
            String ansver = null;
            String funcname = opts.getFuncName();
            if (opts.getPermittable()) {
                try {ansver = ((xmppbot.funcs.func) Class.forName("xmppbot.funcs." + funcname).newInstance()).process(opts);
                } catch (InstantiationException ex) {ex.printStackTrace();} catch (ClassNotFoundException ex) {
                    ex.printStackTrace();
                } catch (IllegalAccessException ex) {ex.printStackTrace();}
            } else {
                try {ansver = ((xmppbot.funcs.func) Class.forName("xmppbot.funcs." + funcname).newInstance()).processnotallowed(opts);
                } catch (InstantiationException ex) {ex.printStackTrace();} catch (ClassNotFoundException ex) {
                    ex.printStackTrace();
                } catch (IllegalAccessException ex) {ex.printStackTrace();}
                if (ansver == null) {
                    ResultSet rs = MySQLDB.getStatement().executeQuery("SELECT * FROM nonpermansvers;");
                    ArrayList ansvers = new ArrayList();
                    while (rs.next()) {
                        ansvers.add(rs.getString("ansver"));
                    }
                    int id = Main.rand.nextInt(ansvers.size());
                    ansver = (String) ansvers.get(id);
                    opts.setAnsverable(true);
                    db.killrs(rs); rs = null;
                }
            }
            opts.setAnsver(ansver);
        }
    }
}
